const db = uniCloud.database();
const { result } = require("xzs-utils");
module.exports = {
	_before: async function() {
		// 通用预处理器
		this.startTime = Date.now();
		console.log(this.getHttpInfo(), 'this.getHttpInfo()');
		this.query = this.getHttpInfo().query || {};
	},
	get: async function() {
		const {
			page = 1, // 页码，默认为1
				pageSize = 10, // 每页数量，默认为10
				status = 1,
		} = this.query;
		const collection = await db
			.collection("mysite-news")
			.where({
				article_status: status, // 只查询已发布的文章
			})
			.field({
				view_count: true,
				article_status: true,
				title: true,
				avatar: true,
				description: true,
				publish_date: true
			});
		// 获取总数
		const countResult = await collection.count();
		const total = countResult.total;
		// 分页查询
		const data = await collection
			.skip((page - 1) * pageSize)
			.limit(pageSize)
			.get();
		return result(
			0,
			"success", {
				list: data.data,
				page,
				pageSize,
				status,
			},
			total
		);
	},
	_after: function(error, result) {
		if (error) {
			throw error; // 如果方法抛出错误，也直接抛出不处理
		}
		result.timeCost = Date.now() - this.startTime;
		return result;
	},
};